CC = gcc
LEX = flex
YACC = bison -y
TARGETS = gax gaxasm gaxld gaxgen gaxasm.yy.c gaxasm.y.tab.c gaxasm.y.tab.h gaxasm.y.output gaxld.yy.c gaxld.y.tab.c gaxld.y.tab.h gaxld.y.output gaxgen.yy.c gaxgen.y.output gaxgen.y.tab.c gaxgen.y.tab.h hello echo fnord
OBJECTS = alu.o control.o pc.o io.o memory.o registers.o instruction.o
GAXASM = $(if $(shell whereis gaxasm | grep '/bin/gaxasm\b'), gaxasm, ./gaxasm)
GAXLD = $(if $(shell whereis gaxld | grep '/bin/gaxld\b'), gaxld, ./gaxld)
GAXGEN = $(if $(shell whereis gaxgen | grep '/bin/gaxgen\b'), gaxgen, ./gaxgen)

all: gax gaxasm gaxld gaxgen

### FOR THE SIMULATOR

gax: gax.c $(OBJECTS) version.h
	$(CC) -o gax -pthread gax.c $(OBJECTS) -lm -lpthread

alu.o: alu.c alu.h type.h opcode.h
	$(CC) -c -o alu.o alu.c

control.o: control.c control.h type.h opcode.h
	$(CC) -c -o control.o control.c

pc.o: pc.c pc.h instruction.h type.h
	$(CC) -c -o pc.o pc.c

io.o: io.c io.h type.h
	$(CC) -c -o io.o -pthread io.c

memory.o: memory.c memory.h type.h
	$(CC) -c -o memory.o memory.c

instruction.o: instruction.c instruction.h type.h
	$(CC) -c -o instruction.o instruction.c

registers.o: registers.c registers.h type.h
	$(CC) -c -o registers.o registers.c

### END SIMULATOR 

### FOR THE ASSEMBLER 

gaxasm: gaxasm.y.tab.c gaxasm.y.tab.h gaxasm.yy.o opcode.o directive.o registers.o symboltable.o 
	$(CC) -o gaxasm gaxasm.y.tab.c gaxasm.yy.o opcode.o directive.o registers.o symboltable.o -lm

opcode.o: opcode.c opcode.h
	$(CC) -c -o opcode.o opcode.c

symboltable.o: symboltable.c symboltable.h
	$(CC) -c -o symboltable.o symboltable.c

directive.o: directive.c directive.h gaxasm.y.tab.h
	$(CC) -c -o directive.o directive.c

gaxasm.yy.o: gaxasm.yy.c gaxasm.y.tab.h opcode.h
	$(CC) -c -o gaxasm.yy.o gaxasm.yy.c

gaxasm.y.tab.c: gaxasm.y opcode.h registers.h
	$(YACC) -v -d -b gaxasm.y gaxasm.y

gaxasm.yy.c: gaxasm.l gaxasm.y.tab.h opcode.h registers.h
	$(LEX) -o gaxasm.yy.c gaxasm.l

gaxasm.y.tab.h: gaxasm.y.tab.c

### END ASSEMBLER

### FOR THE LINKER 

gaxld: gaxld.yy.o gaxld.y.tab.c gaxld.y.tab.h symboltable.o
	$(CC) -o gaxld gaxld.y.tab.c gaxld.yy.o symboltable.o

gaxld.y.tab.c: gaxld.y 
	$(YACC) -v -d -b gaxld.y gaxld.y

gaxld.yy.o: gaxld.yy.c gaxld.y.tab.h
	$(CC) -c -o gaxld.yy.o gaxld.yy.c

gaxld.yy.c: gaxld.l gaxld.y.tab.h
	$(LEX) -o gaxld.yy.c gaxld.l

gaxld.y.tab.h: gaxld.y.tab.c

### END LINKER

### CODE GENERATOR

gaxgen: gaxgen.yy.o gaxgen.y.tab.c gaxgen.y.tab.h symboltable.o version.h
	$(CC) -o gaxgen gaxgen.y.tab.c gaxgen.yy.o symboltable.o

gaxgen.y.tab.c: gaxgen.y 
	$(YACC) -v -d -b gaxgen.y gaxgen.y

gaxgen.yy.o: gaxgen.yy.c gaxgen.y.tab.h
	$(CC) -c -o gaxgen.yy.o gaxgen.yy.c

gaxgen.yy.c: gaxgen.l gaxgen.y.tab.h
	$(LEX) -o gaxgen.yy.c gaxgen.l

gaxgen.y.tab.h: gaxgen.y.tab.c

### END CODE GENERATOR

install: gax gaxasm gaxld gaxgen
	install -m 755 -T gax /usr/local/bin/gax	
	install -m 755 -T gaxasm /usr/local/bin/gaxasm
	install -m 755 -T gaxld /usr/local/bin/gaxld
	install -m 755 -T gaxgen /usr/local/bin/gaxgen

clean: 
	rm -f *.tmp *.o $(TARGETS)

hello: test.o print_number.o
	$(GAXLD) test.o print_number.o | $(GAXGEN) > hello

print_number.o: print_number.s
	$(GAXASM) print_number.s > print_number.o

test.o: test.s
	$(GAXASM) test.s > test.o

echo: echo.o
	$(GAXLD) echo.o | $(GAXGEN) > echo

echo.o: echo.s
	$(GAXASM) echo.s > echo.o

fnord: fnord.o fnords.o prints.o
	$(GAXLD) fnord.o fnords.o prints.o | $(GAXGEN) > fnord

fnord.o: fnord.s
	$(GAXASM) fnord.s > fnord.o

prints.o: prints.s
	$(GAXASM) prints.s > prints.o

fnords.o: fnords.s
	$(GAXASM) fnords.s > fnords.o

source_size:
	ls -l `svn ls` | egrep '\.(h|c|y|l)$$' | awk '{s+=$$5} END {print s}'

